*** do-file for ejpr paper (forthcoming)
** title: dynamics of protest and electoral politics in the great recession
** authors: björn bremer (bremer@mpifg.de), swen hutter and hanspeter kriesi
** description: analyse the party-level data to create results for the main analysia and appendix
** last updated: 13 October 2019

clear
set more off 
cd "" // set your local working directory which contains the folders with a sub-folder for the data, graphs, tables, and appendix here

**---------------------------------------
*** set the scheme for the graphs
**---------------------------------------

*ssc install blindschemes, replace
set scheme  plottigblind
set scheme plotplainblind

**---------------------------------------
*** load the party-level dataset
**---------------------------------------

use "./data/data_party_replication.dta", clear

**---------------------------------------
*** create new and recode existing variables
**---------------------------------------

** create a misery factor
factor gdp_change unemployment_change debt_change
predict misery

** create a simple election number count
recode election_number (-1 0 = 0) (1 = 1) (2 3 4 5 = 2), gen(election_simple)

** recode electoral loss into positive values
gen elect_loss = vote_change*(-1)

** weight protest for duration of the legislative period
gen wevent_dur = wevent/duration
gen wpart_dur = lpopw_part_all/duration

** standardise variables
egen z2_wevent = std(wevent_dur)
egen z2_wpart = std(wpart_dur)
egen z2_elect_loss = std(elect_loss)
egen z2_vote_change = std(vote_change)
egen z2_misery = std(misery)
egen z2_unemployment_change = std(unemployment_change)
egen z2_debt_change = std(debt_change)
egen z2_gdp_change = std(gdp_change)

** relabel some variables
lab var z2_vote_change "Vote change"
lab var z2_elect_loss "Electoral loss"
lab var misery "Misery"

lab def election_simple 0 "Pre-crisis Elec." 1 "First crisis elec." 2 "Later crisis elec."
lab val election_simple election_simple 

lab var z2_wevent "Protest"
lab var z2_wpart "Protest"
lab var z2_misery "Misery"
lab var z2_unemployment_change "Unempl."
lab var z2_debt_change "Debt"
lab var z2_gdp_change "GDP"

lab var east "Eastern Europe"
lab def east 0 "Not Eastern Europe" 1 "Eastern Europe" 
lab val east east

lab var t_vote "Vote share (t-1)"

**---------------------------------------
*** table 5: vote change as dv with misery as iv
**---------------------------------------

cd "./tables/"

eststo clear

eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0

esttab using table5.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe)

**---------------------------------------
*** create marginal effect plots for the main text
**---------------------------------------

cd "../graphs/"

** figure 3: plot average marginal effect of misery of mainstream vs. non-mainstream party
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
margins party_mainstream, dydx(z2_wevent) vsquish
marginsplot, recast(scatter) recastci(rspike) ///
yline(0) xlabel(-.25 " " 0 "Non-mainstream party" 1 "Mainstream party" 1.25 " ", notick nogrid) ylabel(, grid) ///
level(83) aspectratio(1.25) ysize(4) xsize(3.5) title("") xtitle("Party") ytitle("Average marginal effect of protest")
graph export figure3.eps, replace

* figure 4: plot average marginal effect of misery interacted with protest
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
margins party_mainstream, dydx(z2_misery) at(z2_wevent=(-1 (0.1) 6.5)) vsquish
marginsplot, xsize(7.5) ysize(5) xlabel(-1 (1) 6.5) yline(0) level(83) ytitle("Average marginal effect of misery") title("") recast(line) recastci(rarea) ciopts(color(*.5))
graph export figure4.eps, replace

**---------------------------------------
*** appendix: robustness tests
**---------------------------------------

cd "../appendix"

**---------------------------------------
** table b-3: estimate models with count instead of participatints 

cd "./tables/"

eststo clear
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wpart##i.party_mainstream c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wpart##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wpart##i.party_left c.z2_misery if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wpart##i.party_left##c.z2_misery if east == 0

esttab using table_b3.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe (count))

**---------------------------------------
** table b-6: estimate models with unemployment instead of misery 

eststo clear
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_unemployment_change if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_unemployment_change if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_unemployment_change if east == 0
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_unemployment_change if east == 0

esttab using table_b6.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001)  ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe (unemployment))

**---------------------------------------
** table b-11: estimate models with clustered standard errors

eststo clear
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0, cluster(country_num)
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0, cluster(country_num)
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0, cluster(country_num)
eststo: quietly regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0, cluster(country_num)

esttab using table_b11.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe with country-clustered SEs)

**---------------------------------------
** table a-14: estimate models with quantile regression

eststo clear
eststo: quietly qreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
eststo: quietly qreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly qreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
eststo: quietly qreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0

esttab using table_b14.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe (quantile regression))

**---------------------------------------
** table b-17: estimate robust regressions

eststo clear
eststo: quietly rreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
eststo: quietly rreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly rreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
eststo: quietly rreg z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0

esttab using table_b17.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe (robust regression))

**---------------------------------------
** table b-18: explaining the electoral loss of parties relative to their size

gen welect_loss = elect_loss/t_vote
egen z2_welect_loss = std(welect_loss)

eststo clear
eststo: quietly regress z2_welect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream c.z2_misery if east == 0
eststo: quietly regress z2_welect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
eststo: quietly regress z2_welect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
eststo: quietly regress z2_welect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0

esttab using table_b18.rtf, b(2) t(2) r2(2) nogaps nobaselevels label replace ///
starlevels(+ 0.1 * 0.05 ** 0.01 *** 0.001) ///
title(The effect of misery and protest on the electoral loss of different parties in Western Europe)

**---------------------------------------
*** appendix: marginal effect plots
**---------------------------------------

cd "../graphs/"

** fiure c-2: plot the interaction effect the other way round (as recommended by Berry et al. 2012)
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_mainstream##c.z2_misery if east == 0
margins party_mainstream, dydx(z2_wevent) at(z2_misery=(-2.688459 (0.1) 3.770832)) vsquish
marginsplot, xsize(7.5) ysize(5) xlabel(-3 (1) 4) yline(0) level(83) ytitle("Average marginal effect of protest")  title("") recast(line) recastci(rarea) ciopts(color(*.5))
graph export figure_c2.eps, replace

** figure c-4: average marginal effect of misery for left vs. non-left parties
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left c.z2_misery if east == 0
margins party_left, dydx(z2_wevent) vsquish
marginsplot, recast(scatter) recastci(rspike) ///
yline(0) xlabel(-.25 " " 0 "Non-left party" 1 "Left party" 1.25 " ", notick nogrid) ylabel(, grid) ///
level(83) aspectratio(1.25) title("") xtitle("Party") ytitle("Average marginal effect of protest")
graph export figure_c3.eps, replace

* figure c-4: average marginal effect of misery on electoral loss for left vs. non-left parties across the range of protest
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0
margins party_left, dydx(z2_misery) at(z2_wevent=(-1 (0.1) 6.5)) vsquish
marginsplot, xsize(7.5) ysize(5) xlabel(-1 (1) 6.5) yline(0) level(83) ytitle("Average marginal effect of misery") title("") recast(line) recastci(rarea) ciopts(color(*.5))
graph export figure_c4.eps, replace

* figure c-5: average marginal effect of protest on electoral loss of left vs. non-left parties across the range of misery
regress z2_elect_loss i.prime_minister i.government c.z2_wevent##i.party_left##c.z2_misery if east == 0
margins party_left, dydx(z2_wevent) at(z2_misery=(-2.688459 (0.1) 3.770832)) vsquish
marginsplot, xsize(7.5) ysize(5) xlabel(-3 (1) 4) yline(0) level(83) ytitle("Average marginal effect of protest")  title("") recast(line) recastci(rarea) ciopts(color(*.5))
graph export figure_c5.eps, replace
